package com.wzy.mall.web.servlet.admin;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.wzy.mall.model.bo.adminBO.OrderByPageBO;
import com.wzy.mall.model.bo.adminBO.OrderChangeBO;
import com.wzy.mall.model.vo.adminVO.OrderInfoVO;
import com.wzy.mall.model.vo.adminVO.OrderLimitVO;
import com.wzy.mall.model.vo.ResultVO;
import com.wzy.mall.service.admin.OrderService;
import com.wzy.mall.service.admin.impl.OrderServiceImpl;
import com.wzy.mall.utils.HTTPUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class Order {

    private static ObjectMapper jsonMapper = new ObjectMapper();
    private static OrderService orderService = new OrderServiceImpl();


    /**
     *  分页显示订单
     * @param request
     * @param response
     */
    public static void ordersByPage(HttpServletRequest request, HttpServletResponse response) {
        try {
            // 获取JSON数据
            String requestBody = HTTPUtils.getJSONRequest(request);
            // 转换为BO对象
            OrderByPageBO orderByPageBO = jsonMapper.readValue(requestBody, OrderByPageBO.class);

            // 调用Service层
            OrderLimitVO orderLimitVO = orderService.findOrderByPage(orderByPageBO);

            if(orderLimitVO.getOrders() == null) {
                ResultVO resultVO = new ResultVO(10000, null, "服务器繁忙，请稍后再试！！");
                response.getWriter().write(jsonMapper.writeValueAsString(resultVO));
            } else {
                ResultVO resultVO = new ResultVO(0, orderLimitVO, null);
                response.getWriter().write(jsonMapper.writeValueAsString(resultVO));
            }


        } catch (IOException e) {
            e.printStackTrace();
            try {
                ResultVO resultVO = new ResultVO(10000, null, "服务器繁忙，请稍后再试！！");
                response.getWriter().write(jsonMapper.writeValueAsString(resultVO));
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }
    }

    /**
     *  根据ID获取订单信息
     * @param request
     * @param response
     */
    public static void getOrder(HttpServletRequest request, HttpServletResponse response) {
        // 获取键值对参数
        String id = request.getParameter("id");

        try {
            // 调用Service层
            OrderInfoVO orderInfoVO = orderService.getOrder(id);

            ResultVO resultVO = new ResultVO(0, orderInfoVO, null);
            response.getWriter().write(jsonMapper.writeValueAsString(resultVO));
        } catch (IOException e) {
            e.printStackTrace();
            try {
                ResultVO resultVO = new ResultVO(10000, null, "服务器繁忙，请稍后再试！");
                response.getWriter().write(jsonMapper.writeValueAsString(resultVO));
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }

    }

    /**
     *  根据ID删除订单 (后台系统是物理删除)
     * @param request
     * @param response
     */
    public static void deleteOrder(HttpServletRequest request, HttpServletResponse response) {
        // 获取键值对参数
        String id = request.getParameter("id");

        // 调用Service层
        int result = orderService.deleteOrder(id);

        try {
            if(result == 0) {
                ResultVO resultVO = new ResultVO(10000, null, "服务器繁忙，请稍后再试！");
                response.getWriter().write(jsonMapper.writeValueAsString(resultVO));
            } else {
                ResultVO resultVO = new ResultVO(0, null, null);
                response.getWriter().write(jsonMapper.writeValueAsString(resultVO));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    /**
     *  修改订单
     * @param request
     * @param response
     */
    public static void changeOrder(HttpServletRequest request, HttpServletResponse response) {

        try {
            // 获取JSON数据
            String requestBody = HTTPUtils.getJSONRequest(request);
            // 封装成BO对象
            OrderChangeBO orderChangeBO = jsonMapper.readValue(requestBody, OrderChangeBO.class);
            //System.out.println(orderChangeBO);

            // 调用Service层
            int result = orderService.updateOrder(orderChangeBO);
            if(result == 0) {
                ResultVO resultVO = new ResultVO(10000, null, "服务器繁忙啊！！");
                response.getWriter().write(jsonMapper.writeValueAsString(resultVO));
            } else {
                ResultVO resultVO = new ResultVO(0, null, null);
                response.getWriter().write(jsonMapper.writeValueAsString(resultVO));
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
